<?php
	include_once "config_db.php";
	
	$database = 'mymap';
	
	if (!mysql_connect($db_host, $db_user, $db_pwd))
	die("Can't connect to database");
	
	if (!mysql_select_db($database))
	die("Can't select database");
	
	mysql_query("SET NAMES 'utf8'");

	$result = null;
	$function_name = $_REQUEST["function_name"];
	
	if($function_name == "insert_segment") {
		$administrative_id 	=	$_REQUEST["administrative_id"];
		$name_segment 		=	$_REQUEST["name_segment"];
		$start_node			=	$_REQUEST["start_node"];
		$end_node			=	$_REQUEST["end_node"];
		$sequence			=	$_REQUEST["sequence"];
		
		$result = insert_segment($administrative_id, $name_segment, $start_node, $end_node, $sequence);		
	} elseif($function_name == "insert_locality") {
		$city_id			=	$_REQUEST["city_id"];
		$district_id		=	$_REQUEST["district_id"];
		$ward_id			=	$_REQUEST["ward_id"];
		$locality_lv		=	$_REQUEST["locality_lv"];
		$name_locality		=	$_REQUEST["name_locality"];
		$lat				=	$_REQUEST["lat"];
		$lng				=	$_REQUEST["lng"];
		$result = insert_locality($city_id, $district_id, $ward_id, $locality_lv, $name_locality, $lat, $lng);
	} elseif($function_name == "insert_node") {
		$node_name			=	$_REQUEST["node_name"];
		$lat				=	$_REQUEST["lat"];
		$lng				=	$_REQUEST["lng"];
		$wayset				=	$_REQUEST["wayset"];
		$info				=	$_REQUEST["info"];
		$result = insert_node($node_name, $lat, $lng, $wayset,  $info);
	} elseif($function_name == "update_node") {
		$node_id_update		=	$_REQUEST["node_id_update"];
		$wayset_update		=	$_REQUEST["wayset_update"];
		$result = update_node($node_id_update, $wayset_update); 
	} /*elseif($function_name == "insert_segment_detail") {
		$segment_id			=	$_REQUEST["segment_id"];
		$list_point			=	$_REQUEST["list_point"];
		$zoom_level			=	$_REQUEST["zoom_lv"];
		$result = insert_point($segment_id, $list_point, $zoom_level);
	}*/

	function insert_segment($administrative_id, $name_segment, $start_node, $end_node, $sequence) {

		$table = 'segment';
		$sql_query = "INSERT INTO {$table} (administrative_id, name_segment, start_node, end_node, sequence_segment)
					  VALUES (".$administrative_id.", '".$name_segment."', ".$start_node.", '".$end_node."', ".$sequence.")";
		$result = mysql_query($sql_query);
		if($result) {
			echo 1;
		} else {
			echo 0;
		}
	}
	
	function insert_locality($city_id, $district_id, $ward_id, $locality_lv, $name_locality, $lat, $lng) {
	
		$sql_query = "INSERT INTO administrative_map (city_id, district_id, ward_id, locality_level, name, lat, lng) 
					  VALUES (".$city_id.", ".$district_id.", ".$ward_id.", ".$locality_lv.", '".$name_locality."', ".$lat.", ".$lng.")";
		$result = mysql_query($sql_query);
		if($result) {
			echo 1;
		} else {
			echo 0;
		}
	}
	function insert_node($node_name, $lat, $lng, $wayset,  $info) {
	
		$sql_query = "INSERT INTO node (node_name, lat, lng, wayset, info) 
					  VALUES ('".$node_name."', ".$lat.", ".$lng.", '".$wayset."', '".$info."')";
		$result = mysql_query($sql_query);
		echo $result;
		/*if($result) {
			echo 1;
		} else {
			echo 0;
		}*/
	}
	function insert_segment_detail($segment_id, $list_point, $zoom_level) {
		$sql_query = "INSERT INTO topo_segment_detail (segment_id, list_point, zoom_level) 
					  VALUES (".$segment_id.", '".$list_point."', ".$zoom_level.")";
		$result = mysql_query($sql_query);
		if($result) {
			echo 1;
		} else {
			echo 0;
		}
	}
	
	function update_node($node_id_update, $wayset_update) {
		$sql_query = "UPDATE node
					  SET wayset = ".$wayset_update."
					  WHERE id = ".$node_id_update;
		$result = mysql_query($sql_query);
		if($result) {
			echo 1;
		} else {
			echo 0;
		}
	}
?>